﻿using System;
using System.Threading;

namespace Wicture.EPC.OnlineQueryProxy.Agents
{
    public class Agent : IComparable
    {
        public string ConnectionId { get; set; }
        public string Id { get; set; }
        public string Name { get; set; }
        public string GroupName { get; set; }
        public string ProcessorName { get; set; }
        public int Weight { get; set; }
        public bool IsReady { get; set; }
        public bool Enabled { get; set; } // 禁用代理服务
        public bool IsOnline { get; set; }
        public bool ForceOffline { get; set; }      // 强制下线
        public DateTime LastConnectedTime { get; set; }  // 最后一次连接时间。

        private static readonly ReaderWriterLockSlim slimLock = new ReaderWriterLockSlim();
        private bool isBusy;
        public bool IsBusy           // 正在查询，还未返回结果。
        {
            get
            {
                slimLock.EnterReadLock();
                var returnValue = isBusy;
                slimLock.ExitReadLock();

                return returnValue;
            }
            set
            {
                slimLock.EnterWriteLock();
                isBusy = value;
                slimLock.ExitWriteLock();
            }
        }

        public WeightRange WeightRange { get; set; } = new WeightRange();

        public int CompareTo(object obj)
        {
            return Weight.CompareTo((obj as Agent).Weight);
        }
    }

    public class WeightRange
    {
        public decimal Start { get; set; }
        public decimal End { get; set; }
    }

    public class AgentState
    {
        public AgentState()
        {

        }
        public AgentState(Agent agent)
        {
            Id = agent.Id;
            Enabled = agent.Enabled;
        }
        public string Id { get; set; }
        public bool Enabled { get; set; } // 禁用代理服务
    }
}
